#include"Enemies.h"
CEnemies::CEnemies()
{
	list_goomba.pHead=NULL;
	list_goomba.pTail=NULL;
	list_koopa.pHead=NULL;
	list_koopa.pTail=NULL;
	list_pirana.pHead=NULL;
	list_pirana.pTail=NULL;
	ispause=false;
	BePushed_Sound=LoadSound(L"Sound/bepushed.wav");
	BeJumped_Sound=LoadSound(L"Sound/befired.wav");
}
CEnemies::~CEnemies()
{
	
	
	DeleteEnemies();

}
void CEnemies::DeleteEnemies()
{
	NODE_GOOMBA *p;
	while(list_goomba.pHead!=NULL)
	{
		p=list_goomba.pHead;
		list_goomba.pHead=p->next;
		delete p;
	}
	NODE_KOOPA *q;
	while(list_koopa.pHead!=NULL)
	{
		q=list_koopa.pHead;
		list_koopa.pHead=q->next;
		delete q;
	}
	NODE_PIRANA *node;
	while(list_pirana.pHead!=NULL)
	{
		node=list_pirana.pHead;
		list_pirana.pHead=node->next;
		delete node;
	}
}
int CEnemies::CollisionWithMario(bool is_star_man,RECT marioRect,int direction)
{
	NODE_GOOMBA *p;
	int kq;
	p=list_goomba.pHead;
	while(p!=NULL&&p->goomba->bePushed==false)
	{
		kq=p->goomba->CollisionWithMario(is_star_man,marioRect,direction);
		if(kq!=0)
		{
			if(kq==3) PlaySound(BeJumped_Sound);
			else if(kq==1) PlaySound(BePushed_Sound);
			return kq;
		}
		p=p->next;
	}
	NODE_KOOPA *q;
	q=list_koopa.pHead;
	while(q!=NULL)
	{
		kq=q->koopa->CollisionWithMario(is_star_man,marioRect,direction);
		if(kq!=0)
		{
			if(kq==3) PlaySound(BeJumped_Sound);
			else if(kq==1) PlaySound(BePushed_Sound);
			return kq;
		}
		q=q->next;
	}
	NODE_PIRANA *node;
	node=list_pirana.pHead;
	while(node!=NULL)
	{
		kq=node->pirana->CollisionWithMario(is_star_man,marioRect,direction);
		if(kq!=0) return kq;
		node=node->next;
	}
	return 0;
}
void CEnemies::Move(CStaticObjects *StaticObjects,RECT screen)
{
	NODE_GOOMBA *p;
	p=list_goomba.pHead;
	while(p!=NULL)
	{
		p->goomba->Move(StaticObjects,screen);
		p->goomba->ispause=this->ispause;
		p=p->next;
	}
	NODE_KOOPA *q;
	q=list_koopa.pHead;
	while(q!=NULL)
	{
		q->koopa->Move(StaticObjects,screen);
		q->koopa->ispause=this->ispause;
		if(q->koopa->isrun==true)
		{
			CollisionWithGoomba(q->koopa);
		}
		q=q->next;
	}
	
	

}

void CEnemies::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time,int xcamara)
{
	
	NODE_GOOMBA *p;
	int i=0;
	p=list_goomba.pHead;
	while(p!=NULL)
	{
		p->goomba->Render(sprite,image,time,xcamara);
		if(p->goomba->isover==true) i=RemoveNodeGoomba(list_goomba,p);
		p=p->next;
	}
	if(i==1) RemoveHead(list_goomba);
	if(i==2) RemoveTail(list_goomba);
	NODE_KOOPA *q;
	q=list_koopa.pHead;
	while(q!=NULL)
	{
		q->koopa->Render(sprite,image,time,xcamara);
		q=q->next;
	}
	NODE_PIRANA *node;
	node=list_pirana.pHead;
	while(node!=NULL)
	{
		node->pirana->Render(sprite,image,time,xcamara);
		node=node->next;
	}
}
void CEnemies::CollisionWithGoomba(CKoopa *koopa)
{
	NODE_GOOMBA *p;
	p=list_goomba.pHead;
	while(p!=NULL&&p->goomba->bePushed==false)
	{
		if(koopa->CollisionWithGoomba(p->goomba)) p->goomba->beJump=true;
		if(koopa->moveleft==true) p->goomba->touch_left=true;
		else p->goomba->touch_left=false;
		p=p->next;
	}
}
void CEnemies::GetEnemies(Object object)
{
	switch(object.kind)
	{
	case 30: 
		{
			Goomba= new CGoomba(object.x,object.y,object.width,object.height);
			AddGoomba(list_goomba,Goomba);
		}break;
	case 28:
		{
			Koopa= new CKoopa(object.x,object.y,object.width,object.height);
			AddKoopa(list_koopa,Koopa);
		}break;
	case 29:
		{
			Pirana= new CPirana(object.x,object.y,object.width,object.height);
			AddPirana(list_pirana,Pirana);
		}break;
	}
}
void CEnemies::CreateEnemies()
{

}
void AddGoomba(LIST_GOOMBA &list,CGoomba *goomba)
{
	NODE_GOOMBA *p;
	p=new NODE_GOOMBA;
	if(p==NULL) exit(1);
	p->goomba=goomba;
	p->next=NULL;
	if(list.pHead==NULL)
	{
		list.pHead=p;
		list.pTail=list.pHead;
	}
	else
	{
		p->next=list.pHead;
		list.pHead=p;
	}
}
void AddKoopa(LIST_KOOPA &list,CKoopa *koopa)
{
	NODE_KOOPA *p;
	p=new NODE_KOOPA;
	if(p==NULL) exit(1);
	p->koopa=koopa;
	p->next=NULL;
	if(list.pHead==NULL)
	{
		list.pHead=p;
		list.pTail=list.pHead;
	}
	else
	{
		p->next=list.pHead;
		list.pHead=p;
	}
}
void AddPirana(LIST_PIRANA &list,CPirana *pirana)
{
	NODE_PIRANA *p;
	p=new NODE_PIRANA;
	if(p==NULL) exit(1);
	p->pirana=pirana;
	p->next=NULL;
	if(list.pHead==NULL)
	{
		list.pHead=p;
		list.pTail=list.pHead;
	}
	else
	{
		p->next=list.pHead;
		list.pHead=p;
	}
}
int CEnemies::RemoveNodeGoomba(LIST_GOOMBA &list,NODE_GOOMBA *p)
{
	if(p!=list.pHead)
	{
		if(p->next!=NULL)
		{
			p->goomba=p->next->goomba;
			if(p->next->next==NULL)
			{
				NODE_GOOMBA *tam=p->next;
				p->next=NULL;
				list.pTail=p;
				delete tam;
			}
			else
			{
				NODE_GOOMBA *q=p->next->next;
				NODE_GOOMBA *tam=p->next;
				p->next=q;
				delete tam;
			}
			return 0;
		}
		else return 2;
	}
	else return 1;
}
void CEnemies::RemoveHead(LIST_GOOMBA &list)
{
	NODE_GOOMBA *p;
	if(list.pHead!=NULL)
	{
		/*p=list.pHead->next;
		if(p!=NULL)
		{
			list.pHead->goomba=p->goomba;
			list.pHead->next=p->next;
			delete p;
		}
		else
		{
			p=list.pHead;
			list.pTail=NULL;
			list.pHead=NULL;
			delete p;
		}*/
		p=list.pHead;
		if(list.pHead->next!=NULL)
		{
			list.pHead=list.pHead->next;
			delete p;
		}
		else
		{
			p=list.pHead;
			list.pTail=NULL;
			list.pHead=NULL;
			delete p;
		}
	}
}
void CEnemies::RemoveTail(LIST_GOOMBA &list)
{
	NODE_GOOMBA *p;
	p=list.pHead;
	while(p->next!=NULL&&p->next->next!=NULL) p=p->next;
	list.pTail=p;
	p->next=p->next->next;
	delete p->next;
}